package com.zhangtai.modules.service.impl.employee;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.EmployeeBpmDao;

import com.zhangtai.modules.entity.employee.EmployeeBpmEntity;

import com.zhangtai.modules.service.employee.EmployeeBpmService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class EmployeeBpmServiceImpl extends ServiceImpl<EmployeeBpmDao, EmployeeBpmEntity> implements EmployeeBpmService {

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<EmployeeBpmEntity> page = this.page(
                new Page<EmployeeBpmEntity>(pageNum,pageSize),
                new QueryWrapper<EmployeeBpmEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public EmployeeBpmEntity getByIdE(Long id) {
            EmployeeBpmEntity one = this.getOne(new QueryWrapper<EmployeeBpmEntity>().eq("oid", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(EmployeeBpmEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(EmployeeBpmEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            EmployeeBpmEntity entity = new EmployeeBpmEntity();
        entity.setOid(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

//    @Override
//    public EmployeeBpmEntity findByOid(@RequestParam("oid") Long oid) {
//        EmployeeBpmEntity one = this.getOne(new QueryWrapper<EmployeeBpmEntity>().eq("oid", oid).eq("is_delete", 0));
//        return one;
//    }

}